package com.crs.task;

import com.crs.entity.Borrow_record;
import com.crs.mapper.BorrowMapper;
import com.crs.mapper.UserMapper;
import com.crs.service.BorrowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
@Slf4j
public class CheckBorrowRecordsEveryDay {

    @Autowired
    private BorrowMapper borrowMapper;

    @Autowired
    private UserMapper userMapper;

    @Scheduled(cron = "0 0 1 * * ?")
    public void checkHasOverTime(){
        //获取已经逾期的记录
        List<Borrow_record> records = borrowMapper.modifyHasOverTime();

        for (Borrow_record bo:records
             ) {
            //如果罚款金额到达350则封禁用户账号
            if ((bo.getPenaltyAmount().compareTo(new BigDecimal("350"))>=0)){
                //封禁账号
                userMapper.update(bo.getUserId());
            }
            //修改罚款金额累加50
            bo.setPenaltyAmount(bo.getPenaltyAmount().add(new BigDecimal("50")));
            //逾期天数加1
            bo.setOverdueDays(bo.getOverdueDays() + 1);
            borrowMapper.updatePenaltyAmount(bo);
        }
    }
}
